<%@ page language="java" import="com.qipay.admin.utils.SiteHelper" contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.baas.model.ActionDoc" %>
<%@ page import="com.qipay.baas.model.CpInfo" %>
<%@ page import="com.qipay.baas.model.User" %>
<%@ page import="com.qipay.user.entity.UserLevel" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<!doctype html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <jsp:include page="head.jsp"></jsp:include>
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/list.css">
    <script type="text/javascript">
        //document.getElementsByTagName("html")[0].style.width=screen.availWidth/100+"px";
        document.getElementsByTagName("html")[0].style.fontSize=screen.availWidth/200+"px";
    </script>
    <style type="text/css">
        .border{
            border:1px solid red;
        }
    </style>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <% CpInfo cpInfo = (CpInfo) request.getAttribute("detail");%>
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">文档接入列表</h3>
                        </div>
                        <div class="new-group">
                                <label>选择类型：</label>
                                <select id="ddltype" name="type">
                                    <option value="1">支付接口</option>
                                    <option value="2">接口类别</option>
                                </select>
                        </div>
                        <div class="button-group">
                                <% if (request.getParameter("t") != null && request.getParameter("t").endsWith("1")) {%>
                                <a href="${pageContext.request.contextPath}/doc/add?t=1">
                                    <button type="button" class="button back-green" style="font-size: 1rem">添加支付接口</button></a>
                                <%} else {%>
                                <a href="${pageContext.request.contextPath}/doc/add?t=2">
                                    <button type="button" class="button back-green" style="font-size: 1rem">添加接口类别</button></a>
                                <%}%>
                        </div>
                        <!-- /.box-header -->
                        <div class="tableContent" style="font-size: 14px">
                            <table id="" width="100%" cellspacing="0" cellpadding="0" border="1">
                                <thead>
                                <tr>
                                    <th width="3">ID</th>
                                    <th width="6">标题</th>
                                    <th width="10">描述</th>
                                    <% if( request.getParameter("t")!=null&&request.getParameter("t").endsWith("1")){%>
                                    <th width="6">图标</th>
                                <%}%>
                                    <th width="6">排序号</th>
                                    <th width="6">类型</th>
                                    <th width="10">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <% List<ActionDoc> articles = (List<ActionDoc>) request.getAttribute("lst");
                                    User currentUser = (User) session.getAttribute("user");
                                    UserLevel userLevel = SiteHelper.getUserLevelByLevel(currentUser.getLevel());
                                    if (articles != null && articles.size() > 0) {
                                        Iterator<ActionDoc> iter = articles.iterator();
                                        ActionDoc user = null;
                                        while (iter.hasNext()) {
                                            user = iter.next();
                                %>
                                <tr id="user_<%=user.getId()%>">
                                    <td><%=user.getId()%>
                                    </td>
                                    <td><%= user.getTitle()%>
                                    </td>
                                    <td><%=user.getInfo()%>
                                    </td>
                                    <% if( user.getType()==1){%>
                                    <td><img
                                             class="img-polaroid"
                                             src="<%= user.getIcon()%>"
                                    />
                                    </td>
                                    <%}%>
                                    <td><%=user.getOrderNumber()%></td>
                                    <td><%=user.getType() == 1 ? "支付类别" : "文章类别"%></td>

                                    <td>
                                        <% if( user.getType()==1){%>
                                        <a href="${pageContext.request.contextPath}/doc/child?did=<%= user.getId()%>">
                                            <button type="button" class="button back-green" style="font-size: 1rem">子文档</button></a>
                                        <%}%>
                                        <% if (userLevel == UserLevel.ADMIN) {%>
                                        <a name="lnkDelete"
                                           pk="<%= user.getId()%>" href="javascript:;">
                                            <button type="button" class="button back-red" style="font-size: 1rem">删除</button>
                                        </a>
                                        <a href="${pageContext.request.contextPath}/doc/<%= user.getId()%>?t=<%= user.getType()%>">
                                            <button type="button" class="button back-green" style="font-size: 1rem">编辑</button></a>
                                        <%}%>

                                    </td>
                                </tr>

                                <%
                                    }
                                } else {
                                %>
                                <tr>
                                    <td colspan="6">no data.</td>
                                </tr>
                                <% }%>
                                </tbody>
                            </table>
                        </div>
                        <!-- /.box-body -->
                    </div>
                    <!-- /.box -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <jsp:include page="foot.jsp"></jsp:include>
</div>
<script src="<%= request.getAttribute("StaticHost") %>/js/jquery.dataTables.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/dataTables.bootstrap.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/jquery.slimscroll.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/fastclick.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>
<script>
    $(function () {

        var channelState = parseInt("<%= request.getParameter("t")!=null&&request.getParameter("t").endsWith("2")?2:1%>", 10);
        $("#ddltype").val(channelState);
        $("#ddltype").change(function () {
            window.location.href = "${pageContext.request.contextPath}/doc?t="+$(this).val();
        })
        $("[name='lnkDelete']").click(function () {
            var pk = $(this).attr("pk");
            if (confirm("此项下的所有子文档都会删除，确定吗？")) {
                if (pk > 0) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/ajax?n=deldocbyid&id=" + pk,
                        type: "GET",
                        async: true,//true异步 false同步
                        context: this,
                        timeout: 10000,
                        success: function (data) {
                            if (data != null && data == "1") {
                                $("#user_" + pk).hide(100);
                            }
                            else {
                                alert("fail:" + data);
                            }
                        },
                        error: function (d, s, b) {
                            alert("失败:" + b);
                        }
                    });
                }
            }
        });
        $('#example2').DataTable({
            'paging': true,
            'lengthChange': false,
            'searching': false,
            'ordering': true,
            'info': true,
            'autoWidth': false
        });
    })
</script>
</body>
</html>

